import Flutter
import UIKit

public class SwiftFlutterTrackingioPlugin: NSObject, FlutterPlugin {
    public static func register(with registrar: FlutterPluginRegistrar) {
        let channel = FlutterMethodChannel(name: "flutter_trackingio", binaryMessenger: registrar.messenger())
        let instance = SwiftFlutterTrackingioPlugin()
        registrar.addMethodCallDelegate(instance, channel: channel)
    }

    public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        if let args = call.arguments as? Dictionary<String, Any>
        {
            switch call.method {
            case "setDebugMode":
                let debug = args["debug"] as! Bool
                Tracking.setPrintLog(debug)
                result(nil)
                return

            case "initWithKeyAndChannelId":
                let appKey = args["appKey"] as! String
                let channelId = args["channelId"] as! String
                Tracking.initWithAppKey(appKey, withChannelId: channelId)
                result(nil)
                return
                
            case "setRegisterWithAccountID":
                let accountId = args["accountId"] as! String
                Tracking.setRegisterWithAccountID(accountId)
                result(nil)
                return
                
            case "setLoginSuccessBusiness":
                let accountId = args["accountId"] as! String
                Tracking.setRegisterWithAccountID(accountId)
                result(nil)
                return
                
            case "setPayment":
                let transactionId = args["transactionId"] as! String
                let paymentType = args["paymentType"] as! String
                let currencyType = args["currencyType"] as! String
                let currencyAmount = args["currencyAmount"] as! Float
                Tracking.setRyzf(transactionId, ryzfType: paymentType, hbType: currencyType, hbAmount: currencyAmount)
                result(nil)
                return
                
            case "setEvent":
                let eventName = args["eventName"] as! String
                Tracking.setEvent(eventName)
                result(nil)
                return
                
            case "setOrder":
                let transactionId = args["transactionId"] as! String
                let currencyType = args["currencyType"] as! String
                let currencyAmount = args["currencyAmount"] as! Float
                Tracking.setDD(transactionId, hbType: currencyType, hbAmount: currencyAmount)
                result(nil)
                return
                
            case "setAdShow":
                let adPlatform = args["adPlatform"] as! String
                let adId = args["adId"] as! String
                let fill = Int32(args["fill"] as! String) ?? 1
                Tracking.onAdShow(adPlatform, adId: adId, isSuccess: fill)
                result(nil)
                return
                
            case "setAdClick":
                let adPlatform = args["adPlatform"] as! String
                let adId = args["adId"] as! String
                Tracking.onAdClick(adPlatform, adId: adId)
                result(nil)
                return
                
            case "setAppDuration":
                let duration = args["duration"] as! Int
                Tracking.setTrackAppDuration(duration)
                result(nil)
                return
                
            case "setPageDuration":
                let activityName = args["activityName"] as! String
                let duration = args["duration"] as! Int
                Tracking.trackViewName(activityName, duration: duration)
                result(nil)
                return
                
            case "exitSdk":
            result(nil)
                return
                
            default:
                result(FlutterMethodNotImplemented)
            }
        } else {
            result(FlutterError.init(code: "errorSetDebug", message: "data or format error", details: nil))
        }
    }
}
